package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;


import form.ConnectionDB;
import model.Sukien;

public class SukienDAO {
	public static boolean insert(Sukien sukien) throws ClassNotFoundException,
			SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "insert into sukien(sukien_id,tensukien,ngaydienra,ngayketthuc,diadiem,title,title_khongdau,hinh,noidung) values (?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, sukien.getSukienid());
		ps.setString(2, sukien.getTensukien());
		ps.setDate(3, sukien.getNgaydienra());
		ps.setDate(4,sukien.getNgayketthuc());
		ps.setString(5, sukien.getDiadiem());
		ps.setString(6, sukien.getTitle());
		ps.setString(7, sukien.getTitlekhongdau());
		ps.setString(8, sukien.getHinh());
		ps.setString(9, sukien.getNoidung());

		int change = 0;
		change = ps.executeUpdate();
		connection.close();

		if (change > 0) {
			return true;
		} else {

			return false;
		}
	}

	public static ArrayList<Sukien> select() throws ClassNotFoundException,
			SQLException {
		Connection connection = ConnectionDB.getConnection();
		ArrayList<Sukien> listSukien = new ArrayList<>();
		String sql = "select * from sukien";
		PreparedStatement ps = connection.prepareStatement(sql);
		ResultSet resultSet = ps.executeQuery();
		while (resultSet.next()) {
			String sukienid = resultSet.getString("sukien_id");
			String tensukien = resultSet.getString("tensukien");
			Date ngaydienra = resultSet.getDate("ngaydienra");
			Date ngayketthuc = resultSet.getDate("ngayketthuc");
			String diadiem = resultSet.getString("diadiem");
			String title = resultSet.getString("title");
			String titlekhongdau = resultSet.getString("title_khongdau");
			String hinh = resultSet.getString("hinh");
			String noidung = resultSet.getString("noidung");
			listSukien.add(new Sukien(sukienid, tensukien, ngaydienra, ngayketthuc, diadiem, title, titlekhongdau, hinh, noidung));
		}
		connection.close();

		return listSukien;

	}

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException, ParseException {
//		ArrayList<Sukien> listSukien = select();
//		for (Sukien mucsukien : listSukien) {
//			System.out.println(mucsukien.toString());
//		}

	}

}
